package {
	import com.prototype.view.background.BitmapScroller;
	import com.prototype.view.BGRenderer;
	import com.prototype.view.DogView;
	import com.singlecore.base.data.List;
	import com.singlecore.base.util.DisplayUtil;
	import com.singlecore.base.util.StringUtil;
	import flash.display.Sprite;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.geom.Rectangle;
	import flash.ui.Keyboard;
	import flash.utils.getTimer;
	
	/**
	 * ...
	 * @author Santiago.J.Elizalde
	 */
	public class Main extends Sprite {
		
		public static const FLOOR_HEIGHT:int = 320;
		static public const JUMP_POWER:int = 4;
		static public const JUMP_FRAME_APEX:int = 45;
		
		private var dog:DogView;
		
		private var jumping:Boolean;
		private var jumpFrames:uint = 0;
		
		private var ground_scroller:BitmapScroller;
		private var landscape_scroller:BitmapScroller;
		private var sky_scroller:BitmapScroller;
		private var steps:uint;
		private var distance:uint;
		private var platforms:List;
		private var gravity:Number = 0.4;
		
		public function Main():void {
			if (stage) init();
			else addEventListener(Event.ADDED_TO_STAGE, init);
		}
		
		private function init(e:Event = null):void {
			removeEventListener(Event.ADDED_TO_STAGE, init);
			// entry point
			
			stage.scaleMode = StageScaleMode.NO_SCALE;
			
			platforms = new List();
			var renderer:BGRenderer = new BGRenderer();
			ground_scroller = new BitmapScroller(renderer.buffer);
			ground_scroller.scrollSpeed = -4;
			
			sky_scroller = new BitmapScroller(new BMPSky1());
			sky_scroller.scrollSpeed = -4;
			sky_scroller.parallaxAmount = 0.2;
			
			landscape_scroller = new BitmapScroller(new BMPTreeline3());
			landscape_scroller.scrollSpeed = -4;
			landscape_scroller.parallaxAmount = 0.5;
			
			addChild(sky_scroller.display);
			sky_scroller.display.x = 0;
			sky_scroller.display.y = 0;
			
			addChild(landscape_scroller.display);
			landscape_scroller.display.x = 0;
			landscape_scroller.display.y = FLOOR_HEIGHT - 180;
			
			addChild(ground_scroller.display);
			ground_scroller.display.x = -32;
			ground_scroller.display.y = FLOOR_HEIGHT;
			
			stage.addEventListener(Event.ENTER_FRAME, scrollBg);
			stage.addEventListener(KeyboardEvent.KEY_DOWN, onKey);
			
			dog = new DogView();
			addChild(dog);
			dog.lookRight();
			dog.x = (stage.stageWidth/2) - (dog.width/2);
			dog.y = FLOOR_HEIGHT - dog.height/2;
		}
		
		private function onKey(e:KeyboardEvent):void {
			if (e.keyCode == Keyboard.SPACE) {
				dog.jump();
			}
			if (e.keyCode == Keyboard.CONTROL) {
				dog.attack();
			}
		}
		
		private function scrollBg(e:Event):void {
			var t:uint = getTimer();
			
			updateDog();
			dog.update();
			
			for each (var item:Sprite in platforms) {
				item.x += ground_scroller.scrollSpeed;
				if (item.x + item.width < 0) {
					DisplayUtil.remove(item);
					platforms.remove(item);
				}
			}
			
			ground_scroller.scroll();
			landscape_scroller.scroll();
			sky_scroller.scroll();
			
			if (steps % 50 == 0) {
				createPlatform();
			}
			
			steps++;
			if (steps >= 1000) {
				steps = 0;
				distance++;
			}
			
			trace("R: "  + Number((getTimer() - t)).toFixed(2) + "ms || D = " + (distance + steps / 1000) + " Km");
		}
		
		private function updateDog():void {
			//Check Platform Collisions
			for each (var item:Sprite in platforms) {
				var dBounds:Rectangle = dog.getBounds(stage);
				var pBounds:Rectangle = item.getBounds(stage);
				var intersects:Boolean = dBounds.intersects(pBounds)
				//var collisionRect:Rectangle = .intersection();
				if (intersects && dog.forceY >= 0) {
					dog.y = item.y - dog.height+1;
					dog.forceY = 0;
					dog.jumping = false;
					return;
				}
			}
			//Check floor collision
			if (dog.y < FLOOR_HEIGHT) {
				dog.forceY += gravity;
			} else if(dog.forceY > 0) {
				dog.jumping = false;
				dog.forceY = 0;
			}
			//get him down
		}
		
		private function onClick(e:MouseEvent):void {
		}
		
		private function createPlatform():void {
			var plat:Sprite = new Sprite();
			plat.graphics.beginFill(0xCCCCCC, 1);
			plat.graphics.drawRect(0, 0, 75+Math.random()*50, 10);
			plat.graphics.endFill();
			platforms.push(plat);
			stage.addChild(plat);
			plat.x = stage.stageWidth;
			plat.y = 300 - Math.random()*100;
		}
		
	}
	
}